package niit.wei.servlet;

import com.alibaba.fastjson.JSON;
import niit.wei.entity.StudentCount;
import niit.wei.service.StudentService;
import niit.wei.service.impl.StudentServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/StudentCountJsonServlet")
public class StudentCountJsonServlet extends HttpServlet {
    private StudentService studentService = new StudentServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<StudentCount> studentCountList = studentService.getStudentCountList();
        Map<String,Object> map = new HashMap<>();
        List<String> times = new ArrayList<>();
        List<Object> counts = new ArrayList<>();
        for (StudentCount studentCount : studentCountList) {
            Long count = studentCount.getCount();
            String month = studentCount.getMonth();
            times.add(month);
            counts.add(count);
        }
        map.put("times", times);
        map.put("counts", counts);
        String jsonString = JSON.toJSONString(map);
        response.getWriter().write(jsonString);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
